# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.7 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.83.1
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
license=('GPL')
# buid conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         "${MINGW_PACKAGE_PREFIX}-audaspace"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         #"${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-gflags"
         #"${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         $([[ "$CARCH" == "x86_64" ]] && echo "${MINGW_PACKAGE_PREFIX}-openimagedenoise")
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         #"${MINGW_PACKAGE_PREFIX}-opensubdiv"
         #"${MINGW_PACKAGE_PREFIX}-openvdb" # there are missing export symbols in openvdb DLL
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
#optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !debug)
install=blender-${CARCH}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0004-fix-finding-glew.patch
        0005-fix-define-syntax.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0008-mingw-use-X11-like-path-layout.patch
        0011-c++11-defines-__i386__-not-i386.patch
        0012-re-enable-SSE-on-mingw-w64-i686.patch
        0014-libstdc++-not-support-utf-stream.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0017-find-pugixml.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0021-comctl32-v6-for-TaskDialogIndirect.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0025-callback-function-prototype-fix.patch)
sha256sums=('4344623cd238a773cba5634fb8f0a723e93b7e92d2c247c9d297dd8dbf25a7cd'
            '52bfd1092414d37330f946bb643f80dffc533bd0c3dd109ce9b252a1b6fe96d7'
            'c547c988a02195bc0cdfe34a88af58af1ebc253a499eaa75e96ed6b4d3992c54'
            'bcaa5b105463aa2dd9ab0f69ed4d422c23f9ae1a00a93e012c62113489f3db56'
            '87036a005a817bce9edd5fb7f18ba3b7a0b8d309d6f1f9bb0f813c931d889c0d'
            'cb603fd30a2e1421f653a187c3beb8e767d043a0d948213e884095cf0a27a411'
            'd54db0c21a9760146f91bada1a0a1d7d9a08250783a840a7dd4f55c19b26ccd9'
            '03c6e8d1713b5c66cb4d531aad1b48a89afac1dc75434ad2dc2d4ad17ba09b1a'
            '9de3b8c5d50a67d439082abb6a14a2ce924473ed7c1f411a24ccb735774e39dc'
            '6c3e47c8051345bd7c98acdc8ea592889f650498793fd5053728cec4ac659303'
            '4ac8822d7620661f34327155b6d2d4491c3704dae5de025ced89830fe622f492'
            'd5c1c5944ce49982633e5d65cdedfb64d0aaa3bd1c8ae4877c4dfc3e5ae03dac'
            '5ce05eed73408045c78cd1be262393b2c6815b77dd2979964a8090e00a3ce5ac'
            'abaa607ea82f2542e7303eddd405534eb93b5e9e8b23c8568e782d62aa7f7b7b'
            '3b51fa1a7cf3d6afd201efffbbe61ab10ad2dfaba370d95b84214369ba551580'
            '9f94a11de1502ffc20bb1336ebf2405e5d5b30eb34b45bb0eda1f67c3ccf67f0'
            '8be734582b9980ad0a3c5a82c200767ce558084f18e3cca11947991a14b0d68a'
            '5b6da467aaef44691f0c0437926c74d08ee36c8e7fe76c00fc04e50a3064de6c'
            '61c0873f9c661f80eb189b7391a4c4c1eef2f7db4178177ce7ff7c47342384ae')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  [[ -d build_files/cmake/platform/platform_win32_mingw64.cmake ]] && rm build_files/cmake/platform/platform_win32_mingw64.cmake
  patch -p1 -i "${srcdir}"/0001-mingw-buildsystem.patch
  patch -p1 -i "${srcdir}"/0002-fix-mingw-w64-definitions.patch
  patch -p1 -i "${srcdir}"/0004-fix-finding-glew.patch
  patch -p1 -i "${srcdir}"/0005-fix-define-syntax.patch
  patch -p1 -i "${srcdir}"/0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
  patch -p1 -i "${srcdir}"/0008-mingw-use-X11-like-path-layout.patch
  patch -p1 -i "${srcdir}"/0011-c++11-defines-__i386__-not-i386.patch
  patch -p1 -i "${srcdir}"/0012-re-enable-SSE-on-mingw-w64-i686.patch
  patch -p1 -i "${srcdir}"/0014-libstdc++-not-support-utf-stream.patch
  patch -p1 -i "${srcdir}"/0015-mingw-python-like-msvc.patch
  patch -p1 -i "${srcdir}"/0016-mingw-windres.patch
  patch -p1 -i "${srcdir}"/0017-find-pugixml.patch
  patch -p1 -i "${srcdir}"/0019-fix-casts.patch
  patch -p1 -i "${srcdir}"/0020-blendthumb-build.patch
  patch -p1 -i "${srcdir}"/0021-comctl32-v6-for-TaskDialogIndirect.patch
  patch -p1 -i "${srcdir}"/0022-find-llvm-convert-paths.patch
  patch -p1 -i "${srcdir}"/0023-link-errors.patch
  patch -p1 -i "${srcdir}"/0025-callback-function-prototype-fix.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir "${srcdir}"/build-${CARCH}  && cd "${srcdir}"/build-${CARCH}

  # i686 SSE is problematic with Cycles: https://developer.blender.org/T36316
  # .. but commit:
  # https://lists.blender.org/pipermail/bf-blender-cvs/2013-October/059705.html
  # .. breaks the build:
  # undefined reference to `ccl::kernel_cpu_sse2_path_trace` .. etc
  # This is because it prevents the definition of __KERNEL_SSE2__ without having
  # ensured WITH_CYCLES_OPTIMIZED_KERNEL_SSE2 is undefined. Let's hope that the
  # bugs have been fixed as patch 0011 re-enables __KERNEL_SSE2__. If they're not
  # fixed then we'll have to pass ENABLESSE2 to cmake instead, -DWITH_CPU_SSE being
  # the correct one (probably!)
  # [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF -DWITH_CPU_SSE=OFF"
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi
  
  export XR_OPENXR_SDK_ROOT_DIR=${MINGW_PREFIX}

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_AUDASPACE=ON \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_ALEMBIC=ON \
    -DWITH_JACK=OFF \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_OPENVDB=OFF \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=python${_pyver} \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR=${pkgdir} install
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe

  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}"
  ${MINGW_PREFIX}/bin/python -m compileall \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/addons
}
